import { defineStore } from 'pinia';
import { geAllCount } from '@/api/api-notification';
import type { NotificationCountT } from '@/@types/type-notification';

export const useNoticeData = defineStore('notice', {
  state: () => {
    return {
      noticeTotal: 0,
      systemTotal: 0,
      meetingTotal: 0,
      feedbackTotal: 0,
      loopholeTotal: 0,
    };
  },
  actions: {
    updateNoticeTotal() {
      try {
        setTimeout(async () => {
          const res = await geAllCount();
          res?.count?.forEach((item: NotificationCountT) => {
            if (item.source === 'openUBMC-meeting') {
              this.meetingTotal = item.count;
            }
            if (item.source === 'openUBMC-publish') {
              this.systemTotal = item.count;
            }
          });
          if (!res?.count) {
            this.meetingTotal = 0;
            this.systemTotal = 0;
          }
          this.noticeTotal = this.meetingTotal + this.systemTotal;
        }, 500);
      } catch {
        // nothing to do
      }
    },
  },
});
